<?php
class Solution
{
    public function atoi($strings)
    {
        $strLen = strlen($strings);
        if($strLen== 0) {
            return 0;
        }
        $i = 0;
        $newStr = '';
        $intArr = [
            '1','2','3','4','5','6','7','8','9','10','-','+'
        ];

        $specialCode = [
            ' ',
            '-',
            '+',
            '1','2','3','4','5','6','7','8','9','10',
        ];
        while ($i<$strLen) {
            if(empty($strings{$i})) {
                $i++;
                continue;
            }

            if(!in_array($strings{0},$specialCode)) {
                return 0;
            }

            if($strings{$i} == '.') {
                return $newStr;
            }

            if(in_array($strings{$i},$intArr)) {

                $newStr.= $strings{$i};
            }
            $i++;
        }

        $res = 0;
        $newStrLen = strlen($newStr);
        $j=0;
        $ok= 1;
        while ($j<$newStrLen) {
            if($newStr{0} == '-') {
                $ok = -1;
            }
            if($newStr{0} == '+') {
                $j++;
                continue;
            }

            if(empty($newStr{$j})) {
                $j++;
                continue;
            }
            $mod = $newStr{$j} % 10;
            $res = $res * 10 + $mod;
            $j++;
        }
        if($ok < 0) {
            $res = 0 - $res;
        }

        if($res >= pow(2,31) - 1) {
            $res = pow(2,31) -1;
        }

        if($res <= -pow(2,31)) {
            $res = -pow(2,31);
        }
        return $res;
    }

    /**
     * @param String $str
     * @return Integer
     */
    function myAtoi($str)
    {
        if (!$str) {
            return 0;
        }
        $strArr = str_split($str);
        $len = count($strArr);

        $hasNum = false;
        $symbol = '';
        $res = [];
        $point = 0;
        while ($point < $len) {
            $cur = $strArr[$point];

            //1.符号和数字后面必须是连续数字
            if (($hasNum || $symbol)  && !is_numeric($cur)) {
                break;
            }

            //2、空白跳过
            if ($cur == ' ') {
                $point++;
                continue;
            }

            //保留符号
            if ($cur == '-' || $cur == '+') {
                $symbol .= $cur;
                $point++;
                continue;
            }

            //兜底，前面处理过空白和符号了，这里处理不是数字必是其他字符，break
            if (!is_numeric($cur)) {
                break;
            }

            //空白、符号、其他字符都已经处理，走到此处只剩整型
            $hasNum = true;

            //不能以0开头
            if (!$res && ord($cur) == 48) {
                $point++;
                continue;
            }

            //保存结果
            $res[] = $cur;
            $point++;
        }

        if (!$res) {
            return 0;
        }

        $result = implode('', $res);

        $min = -pow(2, 31);
        $max = pow(2, 31) - 1;
        if (count($res) >= 10) {
            if ($symbol == '-' && intval($symbol . $result) < $min) {
                return $min;
            }

            if (($symbol == '+' || !$symbol) && $result > $max) {
                return $max;
            }
        }

        return $symbol == '-' ? $symbol . $result : $result;
    }


}

$strings= "words and 987";
//$strings = "   -42";
//$strings = "42";
//$strings = "words and 987";
$strings = "-3.14159";
$res = (new Solution())->atoi($strings);
var_dump($res);
